﻿using FlyingEye.Devices;
using FlyingEye.Devices.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Validation;

namespace FlyingEye.DeviceServices
{
    public class DeviceClientService : ApplicationService
    {
        private readonly IDeviceClientRepository _deviceClientRepository;

        private readonly IDeviceConfigurationRepository _deviceConfigurationRepository;

        public DeviceClientService(
            IDeviceClientRepository deviceClientRepository,
            IDeviceConfigurationRepository deviceConfigurationRepository)
        {
            _deviceClientRepository = deviceClientRepository;
            _deviceConfigurationRepository = deviceConfigurationRepository;
        }

        /// <summary>
        /// 获取设备的配置
        /// </summary>
        public async Task<List<DeviceConfigurationResponse>> GetConfigurationsAsync(string id)
        {
            var list = await _deviceConfigurationRepository.GetListAsync(item => item.ResourceId == id);

            return ObjectMapper.Map<List<DeviceConfigurationModel>, List<DeviceConfigurationResponse>>(list);
        }

        /// <summary>
        /// 删除客户端
        /// </summary>
        public async Task DeleteAsync(string id)
        {
            await _deviceClientRepository.DeleteAsync(id);
        }

        public async Task<bool> AnyAsync(string id)
        {
            return await _deviceClientRepository.AnyAsync(item => item.Id == id);
        }

        /// <summary>
        /// 获取客户端
        /// </summary>
        public async Task<List<DeviceClientResponse>> GetListAsync()
        {
            var list = await _deviceClientRepository.GetListAsync();

            return ObjectMapper.Map<List<DeviceClientModel>, List<DeviceClientResponse>>(list);
        }

        /// <summary>
        /// 获取客户端
        /// </summary>
        public async Task<DeviceClientResponse> GetAsync(string id)
        {
            var model = await _deviceClientRepository.GetAsync(id);

            return ObjectMapper.Map<DeviceClientModel, DeviceClientResponse>(model);
        }

        /// <summary>
        /// 更新客户端
        /// </summary>
        public async Task<DeviceClientResponse> UpdateAsync(UpdateDeviceClientRequest deviceClient)
        {
            if (deviceClient.Site <= 0)
            {
                throw new AppValidationException("基地编必须大于等于0！");
            }

            deviceClient.ProcessName = deviceClient.ProcessName.Trim();

            if (string.IsNullOrWhiteSpace(deviceClient.ProcessName))
            {
                throw new AppValidationException("工序名称不能为空！");
            }

            var model = await _deviceClientRepository.GetAsync(deviceClient.Id);

            ObjectMapper.Map(deviceClient, model);

            await _deviceClientRepository.UpdateAsync(model);

            return ObjectMapper.Map<DeviceClientModel, DeviceClientResponse>(model);
        }

        /// <summary>
        /// 插入客户端
        /// </summary>
        public async Task<DeviceClientResponse> InsertAsync(CreateDeviceClientRequest deviceClient)
        {
            if (deviceClient.Site <= 0)
            {
                throw new AppValidationException("基地编必须大于等于0！");
            }

            var processName = deviceClient.ProcessName.Trim();

            if (string.IsNullOrWhiteSpace(deviceClient.ProcessName))
            {
                throw new AppValidationException("工序名称不能为空！");
            }

            var model = ObjectMapper.Map<CreateDeviceClientRequest, DeviceClientModel>(deviceClient);

            model = await _deviceClientRepository.InsertAsync(model);

            return ObjectMapper.Map<DeviceClientModel, DeviceClientResponse>(model);
        }
    }
}
